package tyk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tyk.common.MyExceptionEnum;
import tyk.common.Result;
import tyk.pojo.Blog;
import tyk.service.BlogService;

@Controller
@RequestMapping("/")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /*
    * 获得所有文章
    * */
    @GetMapping
    public String getAllBlog(Model model) {

        model.addAttribute("blogList",blogService.getAllBlog());
        return "index";
    }

    /*
    * 根据id获得Blog
    * */
    @GetMapping("blog")
    public String getBlogById(Model model, Integer id) {

        model.addAttribute("blog",blogService.getBlogById(id));
        return "blog_detail";
    }


    /*
    * 上面两个controller的整合优化
    * */
//    @GetMapping
    public String getBlogs(Model model,@RequestParam(value = "id", required = false, defaultValue = "-1") Integer blog_id) {

        if (blog_id == -1) {

            model.addAttribute("blogList",blogService.getAllBlog());
            return "index";
        } else {
            model.addAttribute("blog",blogService.getBlogById(blog_id));
            return "blog_detail";
        }
    }


}
